package com.shengruan.user.action;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

import com.shengruan.admin.jdbc.UserMessageJdbc;
import com.shengruan.admin.vo.UserMessage;
import com.shengruan.common.BaseAction;
import com.shengruan.common.Constants;
import com.shengruan.framework.pager.Pager;

public class UserMessageAction extends BaseAction {

    private UserMessageJdbc userMessageJdbc = (UserMessageJdbc) this.getJdbc(UserMessageJdbc.class);

    public void list() throws Exception {
        int currentPage = 1;
        try {
            currentPage = Integer.parseInt(this.input("currentPage"));
        } catch (Exception ex) {
        }

        Pager pager = new Pager(currentPage, userMessageJdbc.getRows(this.getCurrentUser().getId()), 30);
        List<UserMessage> userMessageList = userMessageJdbc.findWithPage(currentPage, 30, this.getCurrentUser().getId());

        this.setRequest("pager", pager);
        this.setRequest("userMessageList", userMessageList);
        this.forward("/user/user-message-list.jsp");
    }


    public void details() throws Exception {
        String id = this.input("id");
        UserMessage userMessage = userMessageJdbc.getById(Integer.valueOf(id));
        userMessage.setStatus(Constants.STATUS_ACTIVE);
        userMessageJdbc.update(userMessage);
        this.setRequest("userMessage", userMessage);


        int userMessageCount = userMessageJdbc.getRows(this.getCurrentUser().getId(), Constants.STATUS_INACTIVE);
        this.setSession(Constants.USER_MESSAGE_COUNT, userMessageCount);

        this.forward("/user/user-message-details.jsp");

    }

    public void delete() throws Exception {
        String id = this.input("id");

        if (StringUtils.isNotEmpty(id) && NumberUtils.isNumber(id)) {
            userMessageJdbc.deleteById(Integer.valueOf(id), this.getCurrentUser().getId());
        }
        this.redirect("/user/userMessage-list.ac");
    }
}
